/********************************************

char * fgets ( char * str, int num, FILE * stream );
Get string from stream

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is
reached, whichever comes first.
A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.
A null character is automatically appended in str after the characters read to signal the end of the C string.

Parameters	
str			Pointer to an array of chars where the string read is stored.
num			Maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used.
stream		Pointer to a FILE object that identifies the stream where characters are read from.
			To read from the standard input, stdin can be used for this parameter.

Return Value
On success, the function returns the same str parameter.
If the End-of-File is encountered and no characters have been read, the contents of str remain unchanged and a null pointer is returned.
If an error occurs, a null pointer is returned.
Use either ferror or feof to check whether an error happened or the End-of-File was reached.

********************************************

int feof ( FILE * stream );
Check End-of-File indicator

Checks whether the End-of-File indicator associated with stream is set, returning a value different from zero if it is.
This indicator is generally set by a previous operation on the stream that reached the End-of-File.
Further operations on the stream once the End-of-File has been reached will fail until either rewind, fseek or fsetpos is successfully called to set
the position indicator to a new value.

Parameters
stream	Pointer to a FILE object that identifies the stream.

Return Value
A non-zero value is returned in the case that the End-of-File indicator associated with the stream is set.
Otherwise, a zero value is returned.

********************************************

int puts ( const char * str );
Write string to stdout

Writes the C string pointed by str to stdout and appends a newline character ('\n').
The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This final null-character is not copied to stdout.

Using fputs(str,stdout) instead, performs the same operation as puts(str) but without appending the newline character at the end.

Parameters
str		C string to be written.

Return Value
On success, a non-negative value is returned.
On error, the function returns EOF.

*********************************************/
#include <stdlib.h>
#include <stdio.h>

#define ASSERT(p) do{ if(!p) return -1; }while(0) 

#define MAX_NUM 50
#define MAX_LEN 255

int parse_file(char* p,char str[MAX_NUM][MAX_LEN],int* cnt)
{
	FILE* fp;
	int i;

	ASSERT(p);

	printf("parase file...\n");

	if((fp = fopen(p,"r")) == NULL)
	{
		return -1;
	}

	i = 0;
	while(!feof(fp))
	{
		fgets(str[i],MAX_LEN,fp);
		printf("%s",str[i]);
		++i;

	}
	printf("\n");
	*cnt = i;

	return 0;
}

int display_info(char str[MAX_NUM][MAX_LEN],int* cnt)
{
	int i;

	printf("File name list:\n");
	for(i = 0; i < *cnt; ++i)
	{
		printf("%s",str[i]);
	}

	return 0;
}

int main(int argc,char** argv)
{
	char* pFile;
	int num;
	char string_list[MAX_NUM][MAX_LEN];

	//parse option
	while (--argc > 0 && **++argv == '-') {
		while (*++*argv) {
			switch (**argv) {
				case 'f':
					if ((--argc <= 0) ||(pFile=*++argv) == NULL)
					{
						return 0;
					}
					parse_file(pFile,string_list,&num);
					display_info(string_list,&num);
					break;
				default:
					break;
			}
		}
	}

	return 0;
}

